<?php

namespace app\common;
use Throwable;

class RootException extends \Exception
{
    protected $code = 1;

    /**
     * 初始化
     * RootException constructor.
     * @param string $message
     * @param int $code
     * @param Throwable|null $previous
     */
    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }

    /**
     * 异常
     * @param bool $debug
     * @return array
     */
    public function catchError(bool $debug = false)
    {
        // 手动调试
        empty($debug) || pdie($this, true);
        // 默认返回值
        $returnData = ['code' => $this->getCode(), 'msg' => $this->getMessage()];
        // 调试模式
        env('app_debug') && $returnData['errorOffset'] = $this->getFile() . '第' . $this->getLine() . '行';
        // 返回
        return $returnData;
    }
}